//==============================================================================
//
//               Copyright (C) 2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-01-18 19:28:59 $ $Revision: 1.1 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DGZipFile.h"

#include "DInc.h"
#include "DTest.h"


void DGZipFile_test()
{
#ifdef HAVE_DGZIPFILE

  DGZipFile *file = [DGZipFile alloc];
  
  STARTTEST();

  [file init];

  // Test for non-present files
  TEST(![file open :"jdflkasdfklj.dsf.gz" :"r"]);
  TEST([file error] == 2);
  TEST(![file isOpen]);
  
  // Test file writing default strategy and level
  TEST([file open :"test.dat.gz" :"w"]);
  TEST([file isOpen]);
  
  TEST([file writeData    :"Hallo" :3]);
  TEST([file writeByte    :75]);
  TEST([file writeChar    :'a']);
  TEST([file writeDouble  :3.76]);
  TEST([file writeLong    :567890]);
  TEST([file writeShort   :1200]);
  TEST([file writeLine    :"going"]);
  TEST([file writeText    :"gone\n"]);
  
  TEST([file flush]);
  
  [file close];
 
  // Test file reading
  TEST([file open :"test.dat.gz" :"r"]);

  TEST([[file readData :3] bcompare :"Hallo" :3] == 0);
  TEST( [file readByte]   == 75);
  TEST( [file readChar]   == 'a');
  TEST( [file readDouble] == 3.76);
  TEST( [file readLong]   == 567890);
  TEST( [file readShort]  == 1200);
  TEST([[file readLine]  ccompare :"going"] == 0);
  TEST([[file readText]  ccompare :"gone\n"] == 0);

  [file close];

  // Test file writing with strategy and level
  TEST([file open :"test.dat.gz" :"w" :5 :DGZ_HUFFMAN]);
  TEST([file isOpen]);
  
  TEST([file writeData    :"Hallo" :3]);
  TEST([file writeByte    :75]);
  TEST([file writeChar    :'a']);
  TEST([file writeDouble  :3.76]);
  TEST([file writeLong    :567890]);
  TEST([file writeShort   :1200]);
  TEST([file writeLine    :"going"]);
  TEST([file writeText    :"gone\n"]);
  
  TEST([file flush]);
  
  [file close];
 
  // Test file reading
  TEST([file open :"test.dat.gz" :"r"]);

  TEST([[file readData :3] bcompare :"Hallo" :3] == 0);
  TEST( [file readByte]   == 75);
  TEST( [file readChar]   == 'a');
  TEST( [file readDouble] == 3.76);
  TEST( [file readLong]   == 567890);
  TEST( [file readShort]  == 1200);
  TEST([[file readLine]  ccompare :"going"] == 0);
  TEST([[file readText]  ccompare :"gone\n"] == 0);

  TEST( [file seek :(3+1+1) :DGZ_SEEK_SET]); // well...
  TEST( [file tell] == 5);
  
  TEST( [file readDouble] == 3.76);
  
  [file close];

  [file free];

  STOPTEST();
#endif
}

